/**
 * 
 */
package com.myworld.web;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.myworld.distributedLock.DistributedLock;
import com.myworld.distributedLock.DistributedLockHandler;

/** 
 * @ClassName: DistributedLockController 
 * @Description:分布式锁的使用 
 * @author zhuqiang
 * @date 2018年12月21日 下午2:48:43 
 */
@Controller
public class DistributedLockController {
	private Logger logger=LoggerFactory.getLogger(DistributedLockController.class);
	
	@Autowired
	private DistributedLockHandler distributedLockHandler;
	
	@RequestMapping("distributedLockUse")
	public String distributedLockUse(){
		//1. 创建锁对象
		DistributedLock lock=new DistributedLock("lockKey", 5*1000);
		//2.获取分布式锁
		if(distributedLockHandler.tryLock(lock,5*1000 , 5*1000)){
			try{
			//执行业务代码
			logger.info("获取锁成功,执行业务代码....................");
			Thread.sleep(3000);
			}catch(Exception e){
				e.printStackTrace();
			}finally{
				//释放锁对象
				distributedLockHandler.releaseLock(lock);
			}
		}else{
			logger.info("获取锁失败,....................");
		}
		return "success";
	}
}
